﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Interfaces;

namespace AliorSync
{
    [Export(typeof(ICsvFile))]
    public class AliorSyncCsvFile : CsvFileBase
    {
        public AliorSyncCsvFile()
        {
            Transactions = new List<Transaction>();
            BankName = "AliorSync";
        }

        public override void ReadFromCsv(string path)
        {
            Transactions.Clear();
            using (StreamReader reader = new StreamReader(path))
            {
                string line = reader.ReadLine();
                bool firstLine = true;
                while (string.IsNullOrWhiteSpace(line) == false)
                {
                    if (firstLine == false)
                    {
                        string[] parts = line.Split(new[] { ',' });
                        Transaction transaction = new Transaction();
                        transaction.Date = DateTime.ParseExact(parts[0], "dd-MM-yyyy", null);
                        transaction.PayMode = parts[3].StartsWith("Transakcja kartą debetową;")
                            ? PayMode.DebitCard
                            : PayMode.MoneyTransfer;
                        transaction.Info = transaction.PayMode == PayMode.DebitCard
                            ? parts[3].Substring(parts[3].IndexOf(";") + 1)
                            : parts[3];
                        transaction.Memo = transaction.Info;
                        transaction.PayeeName = parts[1];
                        transaction.Amount = decimal.Parse(parts[4].Replace(" PLN", string.Empty),
                            new CultureInfo("en-GB"));
                        transaction.Tags.Add(DateTime.Now.AddMonths(-1).ToString("MM-yyyy"));
                        Transactions.Add(transaction);
                    }
                    else
                    {
                        firstLine = false;
                    }
                    line = reader.ReadLine();
                }
            }
        }
    }
}
